package com.tiandang.controller;

import com.lly835.bestpay.utils.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tiandang.core.error.BusinessException;
import tiandang.core.form.ProductForm;
import tiandang.core.response.CommonReturnType;
import tiandang.core.service.ProductService;
import tiandang.core.service.ProductStockService;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/product")
public class ProductController {

  private final Logger log = LoggerFactory.getLogger(this.getClass());

  @Autowired
  private ProductService productService;

  @Autowired
  private ProductStockService productStockService;


  @GetMapping("/getList")
  public CommonReturnType getList(@RequestParam(value = "name", required = false) String name,
                                  @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                  @RequestParam(value = "limit", defaultValue = "10") Integer limit) {

    return CommonReturnType.success(productService.getList(name, currentPage, limit));

  }

  @PostMapping("/add")
  public CommonReturnType add(ProductForm productForm) throws BusinessException {
    log.info(" 添加商品:productForm: {}" + JsonUtil.toJson(productForm));
    productService.createProduct(productForm);
    return CommonReturnType.success();
  }

  @PostMapping("/update")
  public Object update(ProductForm productForm) throws BusinessException {
    log.info(" 修改商品:productForm: {}" + JsonUtil.toJson(productForm));
    productService.modifyProduct(productForm);
    return CommonReturnType.success();
  }


  @PostMapping("/delete")
  public Object delete(Integer id) {
    productService.delete(id);
    return CommonReturnType.success();
  }

  @GetMapping("/getDetail")
  public Object getDetail(Integer id) {
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("product", productService.getProductById(id));
    resultMap.put("productStock", productStockService.getProductStockByProductId(id));
    return CommonReturnType.success(resultMap);
  }

  // 上线/下线
  @PostMapping("/setDownline")
  public Object setDownline(@RequestParam(value = "id") Integer id, @RequestParam(value = "downline") int downline) {
    productService.setDownline(id, downline);
    return CommonReturnType.success();
  }

  @GetMapping("/getProductStock")
  public Object getProductStock(Integer productId) {
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("productStock", productStockService.getProductStockByProductId(productId));
    return CommonReturnType.success(resultMap);
  }

  @GetMapping("/getAllNormal")
  public CommonReturnType getAllNormal() {
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("productList", productService.getAllNormal());
    return CommonReturnType.success(resultMap);
  }
}
